package com.uzhie.web.service.listener;

import com.uzhie.component.VideoComponent;
import com.uzhie.jt808.jt1078.entity.JT_9206;
import com.uzhie.web.module.Video;
import io.netty.buffer.ByteBufUtil;
import org.springframework.beans.factory.annotation.Autowired;

import javax.jms.BytesMessage;
import javax.jms.Message;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

/**
 * Created by TangFei on 2018/10/11.
 */
public class MQFTPReceiveService implements MQReceiveService {

    @Autowired
    private VideoComponent videoComponent;

    @Override
    public void onMessage(Message message) {
        LOGGER.fatal("收到FTP文件上传信息");
        handlerFtpUp(message);
    }

    private void handlerFtpUp(Message msg) {
        try {
            BytesMessage bytesMessage = (BytesMessage) msg;
            byte[] body = new byte[(int) bytesMessage.getBodyLength()];
            bytesMessage.readBytes(body);
            LOGGER.info("收到文件上传消息体：" + ByteBufUtil.hexDump(body));
            JT_9206 jt_9206 = new JT_9206();
            jt_9206.ReadFromBytes(body);
            Video video = new Video();
            video.setStoragePosition(jt_9206.getFilePath());
            DateFormat dateFormat = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
            video.setBeginTime(dateFormat.parse(jt_9206.getStartTime()));
            video.setEndTime(dateFormat.parse(jt_9206.getEndTime()));
            video.setChannel(jt_9206.getChannel() & 0XFF);
            video.setStatus(1);
            videoComponent.addVideo(video);
        } catch (Exception e) {
            LOGGER.error("FTP文件上传完成消息解析异常", e);
        }
    }
}
